package com.such.kit.mock.data.bean;

import com.such.kit.validate.ValidateCommons;

public class MockDataRule {

	public static final String RULE_TYPE_SEQUENCE_KEY = "sequence";
	public static final String RULE_TYPE_SEQUENCE = "#[sequence]";
	public static final String RULE_TYPE_NUMBER_KEY = "number";
	public static final String RULE_TYPE_NUMBER = "#[number]";
	public static final String RULE_TYPE_RANDOM_KEY = "random";
	public static final String RULE_TYPE_RANDOM = "#[random]";
	public static final String RULE_TYPE_SCOPE_KEY = "scope";
	public static final String RULE_TYPE_SCOPE = "#[scope]";

	public static final String PAD_MODE_LEFT = "left";
	public static final String PAD_MODE_RIGHT = "right";

	public static final String[] RULE_TYPE_RANDOM_VALUE_SCOPE = new String[] {
			"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
	};

	/** ruleKey：定义规则 key，可以在 expression 中通过占位符（#[key]）的方式引用 */
	private String ruleKey;
	private String ruleType;
	private int length;
	private boolean padable;
	private String padMode = PAD_MODE_LEFT;
	private String[] valueScopes;

	public MockDataRule(String ruleKey, String ruleType, String... valueScopes) {
		this.ruleKey = ruleKey;
		this.ruleType = ruleType;
		if (ValidateCommons.isNotEmpty(valueScopes)) {
			this.valueScopes = valueScopes;
		}
	}

	public MockDataRule(String ruleKey, String ruleType, int length, boolean padable, String padMode, String... valueScopes) {
		this(ruleKey, ruleType, valueScopes);
		this.length = length;
		this.padable = padable;
		this.padMode = padMode;
	}

	public String getRuleKey() {
		return this.ruleKey;
	}
	
	public String getRuleType() {
		return this.ruleType;
	}
	
	public int getLength() {
		return this.length;
	}
	
	public boolean padable() {
		return this.padable;
	}

	public String getPadMode() {
		return this.padMode;
	}

	public String[] getValueScopes() {
		return this.valueScopes;
	}

	/**
	 * <p>方法名：sequence</p>
	 * <p>
	 * 描述：默认的 Sequence<br>
	 * key > sequence<br>
	 * length > 3<br>
	 * padable > true
	 * </p>
	 * @return MockDataRule
	 */
	public static MockDataRule sequence() {
		return sequence(MockDataRule.RULE_TYPE_SEQUENCE_KEY, 3);
	}

	/**
	 * <p>方法名：sequence</p>
	 * <p>
	 * 描述：简单的 Sequence<br>
	 * padable > true
	 * </p>
	 * @param ruleKey
	 * @param length
	 * @return MockDataRule
	 */
	public static MockDataRule sequence(String ruleKey, int length) {
		return new MockDataRule(ruleKey, MockDataRule.RULE_TYPE_SEQUENCE_KEY, length, true, PAD_MODE_LEFT);
	}

	/**
	 * <p>方法名：number</p>
	 * <p>
	 * 描述：默认的 Number<br>
	 * key > number<br>
	 * length > 3
	 * </p>
	 * @return MockDataRule
	 */
	public static MockDataRule number() {
		return number(MockDataRule.RULE_TYPE_NUMBER_KEY, 3);
	}

	/**
	 * <p>方法名：number</p>
	 * <p>描述：简单的 Number</p>
	 * @param ruleKey
	 * @param length
	 * @return MockDataRule
	 */
	public static MockDataRule number(String ruleKey, int length) {
		return new MockDataRule(ruleKey, MockDataRule.RULE_TYPE_NUMBER_KEY, length, false, PAD_MODE_LEFT);
	}

	/**
	 * <p>方法名：random</p>
	 * <p>
	 * 描述：默认的 Random<br>
	 * key > random<br>
	 * length > 6<br>
	 * valueScopes > [0-9][A-Z][a-z]
	 * </p>
	 * @return MockDataRule
	 */
	public static MockDataRule random() {
		return random(MockDataRule.RULE_TYPE_RANDOM_KEY, 6);
	}

	/**
	 * <p>方法名：random</p>
	 * <p>
	 * 描述：简单的 Random<br>
	 * valueScopes > [0-9][A-Z][a-z]
	 * </p>
	 * @param ruleKey
	 * @param length
	 * @return MockDataRule
	 */
	public static MockDataRule random(String ruleKey, int length) {
		return new MockDataRule(ruleKey, MockDataRule.RULE_TYPE_RANDOM_KEY, length, false, PAD_MODE_LEFT, MockDataRule.RULE_TYPE_RANDOM_VALUE_SCOPE);
	}

	/**
	 * <p>方法名：scope</p>
	 * <p>
	 * 描述：默认的 Scope<br>
	 * key > scope<br>
	 * length > 1（重复 1 次）<br>
	 * </p>
	 * @param valueScopes
	 * @return MockDataRule
	 */
	public static MockDataRule scope(String... valueScopes) {
		return scope(MockDataRule.RULE_TYPE_SCOPE_KEY, 1, valueScopes);
	}

	/**
	 * <p>方法名：scope</p>
	 * <p>描述：简单的 Scope</p>
	 * @param ruleKey
	 * @param time 重复次数
	 * @param valueScopes
	 * @return
	 */
	public static MockDataRule scope(String ruleKey, int time, String... valueScopes) {
		return new MockDataRule(ruleKey, MockDataRule.RULE_TYPE_SCOPE_KEY, time, false, PAD_MODE_LEFT, valueScopes);
	}

}
